<?php

namespace App\Http\Controllers\Wechat;

use App\Http\Controllers\Controller;
use App\Models\ResourceBusinessName;
use App\Models\ResourceSystemType;
use App\Models\ResourceSystemTypeIntention;
use App\Models\ResourceTotal;
use App\Validate\ResourceSystemTypeValidate;

/**
 * 获取系统栏目列表
 */
class ResourceSystemListController extends Controller
{
    public $model = null;
    public $bookModel = null;
    public $validate = null;

    public function __construct()
    {
        parent::__construct();

        $this->model = new ResourceSystemType();
        $this->bookModel = new ResourceSystemTypeIntention();
        $this->validate = new ResourceSystemTypeValidate();
    }


    /**
     * 获取系统栏目列表
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     */
    public function getSystemTypeList()
    {
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;

        $res = $this->model->lists(['is_play' => 1, 'limit' => $limit]);
        if ($res['data']) {
            $res = $this->disPageData($res);
            return $this->returnApi(200, '获取成功', true, $res);
        }
        return $this->returnApi(203, '暂无数据');
    }

    /**
     * 根据系统栏目id获取书籍列表 （只对 way为 1,3,5有效，对于2、4的直接获取书单、每日推荐列表） 书籍获取方式 1、筛选书籍  2、选择书单 3、选择书商  4、每日荐书  5 、自定义封面并选择书籍
     * @param resource_system_type_id  系统栏目id
     * @param big_type_id 系统大类id
     * @param page 页数    默认第一页
     * @param limit 限制条数   默认 10 条
     */
    public function getSystemTypeBookList()
    {
        //增加验证场景进行验证
        if (!$this->validate->scene('wx_get_system_type_book_list')->check($this->request->all())) {
            return $this->returnApi(201,  $this->validate->getError());
        }

        $resource_system_type_id = $this->request->resource_system_type_id;
        $big_type_id = $this->request->big_type_id;
        $page = intval($this->request->input('page', 1)) ?: 1;
        $limit = intval($this->request->input('limit', 10)) ?: 10;
        $resource_system_type_info = $this->model->where('id', $resource_system_type_id)->where('is_del', 1)->first();
        if (empty($resource_system_type_info)) {
            return $this->returnApi(203, '暂无数据');
        }
        if (empty($big_type_id)) {
            if (in_array($resource_system_type_info['template'], [1, 2, 3, 4, 5, 6, 7])) {
                $big_type_id = "1, 2, 4, 5";
            } elseif (in_array($resource_system_type_info['template'], [8])) {
                $big_type_id = 3;
            }
        } else {
            $big_type_id = $big_type_id;
        }
        if ($resource_system_type_info['way'] == 3) {
            //  $resourceTotalModel = new ResourceTotal();
            // $res = $resourceTotalModel->getResourceListByBusinessNameId(null, $resource_system_type_info['resource_business_id'], $this->request->big_type_id, $resource_system_type_info['resource_business_type_id'], 1, null,$limit);
            $resourceBusinessTypeModel = new ResourceBusinessName();
            $res = $resourceBusinessTypeModel->getResourceBusinessDataByBusinessId(null, $resource_system_type_info['resource_business_id'], $big_type_id, $resource_system_type_info['resource_business_type_id'], $page, $limit);
            if ($res['data']) {
                foreach ($res['data'] as $key => $val) {
                    $res['data'][$key]['browse_num'] = !empty($val['browse_num']) ? format_number($val['browse_num']) : null; //位数转换
                }
                $res = $this->disPageData($res);
                return $this->returnApi(200, '获取成功', true, $res);
            }
        } elseif ($resource_system_type_info['way'] == 1 || $resource_system_type_info['way'] == 5) {
            //以资源商显示数据
            $res = $this->bookModel->lists(['resource_system_type_id' => $resource_system_type_id, 'big_type_id' => $big_type_id, 'limit' => $limit]);
            if ($res['data']) {
                $res = $this->disPageData($res);
                return $this->returnApi(200, '获取成功', true, $res);
            }
        } else {
            return $this->returnApi(201, '参数错误');
        }
        return $this->returnApi(203, '暂无数据');
    }
}
